package demo4.mapper;


import demo4.pojo.User;
import demo4.util.IDBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Mapper是数据持久层的类，作用是和数据库进行操作，提供基础的增删改查等相关操作。
 *
 *
 * UserMapper该类就是和数据库中一个名为User的表进行操作的
 */
@Component
public class UserMapper implements IUserMapper{

    @Autowired
    private IDBUtil dbUtil;// 注入IDBUtil

    public int insert(User user){
        System.out.println("UserMapper:插入了用户数据");
        // 获取数据库链接
        try ( Connection connection = dbUtil.getConnection();){
           String sql = "insert into user(username,password,nickname,age) values(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);// 3.创建PreparedStatement对象 --->sql语句
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getNickname());
            ps.setInt(4,user.getAge());
            return ps.executeUpdate();
        } catch (Exception e) {
           e.printStackTrace();
        }
        return 0;
    }
    @Override
    public User selectByUsername(String username) {
        /*
            1:链接数据库
            2:执行SELECT语句
            3:将查询出的用户数据以一个User对象形式表示
         */
        try (Connection connection = dbUtil.getConnection()){
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user " +
                    "WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){//如果为true，说明该用户存在
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public  int update(User user){
        try (Connection connection = dbUtil.getConnection()){
            String sql = "update user set password=?,nickname=?,age=? where username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,user.getPassword());
            ps.setString(2,user.getNickname());
            ps.setInt(3,user.getAge());
            ps.setString(4,user.getUsername());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public List<User> selectAll() {
        List<User> list = new ArrayList<>();// 创建一个集合，用于存储查询出的用户数据
        try (Connection connection = dbUtil.getConnection()){// 1.链接数据库
           String sql = "select id,username,password,nickname,age from user";
          Statement ps = connection.createStatement();
            ResultSet rs = ps.executeQuery(sql);// 2.执行SELECT语句

            while(rs.next()){// 3.将查询出的用户数据以一个User对象形式表示
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setNickname(rs.getString("nickname"));
                user.setAge(rs.getInt("age"));
                list.add(user);

            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
       return list;
    }
    public int deleteByUsername(String username){
        try (Connection connection = dbUtil.getConnection()){
            String sql = "delete from user where username=?";
           PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,username);
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


}
